package com.amigo.online.message.controller;

import com.alibaba.fastjson.JSONObject;
import com.amigo.online.common.base.controller.BaseController;
import com.amigo.online.message.entity.MIMCResponse;
import com.amigo.online.message.enums.MIMCUrlEnum;
import com.amigo.online.message.util.MIMCHttpUtil;
import com.amigo.online.common.core.response.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.Data;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

/**
 * 历史消息
 */
@Api( "历史消息")
@RestController
@RequestMapping("/history")
public class HistoryMessageController{
    /**
     * 获取单聊历史消息
     * 注：不能用get 用get转码时token的+号会变成空格
     * 如果必须用get，可以把token放在请求头中
     * @param token
     * @param userId 发送者ID
     * @param toUserId 接收者ID
     * @param lastMsgtime 最后时间
     */
    @ApiOperation(value = "获取单聊历史消息" ,  notes="获取单聊历史消息")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "token", value = "token"),
            @ApiImplicitParam(name = "userId", value = "发消息用户ID"),
            @ApiImplicitParam(name = "toUserId", value = "收消息用户ID"),
            @ApiImplicitParam(name = "lastMsgtime", value = "lastMsgtime")
    })
    @PostMapping("/p2p/historyMessage")
    public Result p2pHistoryMessage(String token, String userId, String toUserId, String lastMsgtime){
        RestTemplate restTemplate = new RestTemplate();
        HttpHeaders headers = MIMCHttpUtil.getTokenHeaders(token);
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("toAccount",toUserId);
        jsonObject.put("fromAccount",userId);
        jsonObject.put("utcToTime",lastMsgtime);
        jsonObject.put("count","20");

        HttpEntity<JSONObject> requestEntity = new HttpEntity<JSONObject>(jsonObject, headers);
        try{
            ResponseEntity<HistoryMessageResponse> response =  restTemplate.exchange(MIMCUrlEnum.HISTORY_MESSAGE.getValue(), HttpMethod.POST , requestEntity , HistoryMessageResponse.class);
            HistoryMessage historyMessage =  response.getBody().getData();
            return Result.success(historyMessage.getMessages());
        }catch (Exception e){
            return Result.error();
        }

    }
}
@Data
class HistoryMessageResponse extends MIMCResponse{
    private HistoryMessage data;
}
@Data
class HistoryMessage{

    private long appId;
    private int row;
    private List<Message> messages;
    private String timestamp;
}
@Data
class Message{
    private String sequence;
    private String payload;
    private String ts;
    private String fromAccount;
    private String toAccount;
    private String bizType;
    private String extra;
}